package com.ptmall.app.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.ptmall.app.BuildConfig;
import com.ptmall.app.MainActivity;
import com.ptmall.app.R;
import com.ptmall.app.base.BaseMvpActivity;
import com.ptmall.app.bean.GGBean;
import com.ptmall.app.net.HttpMethods;
import com.ptmall.app.net.api.ApiDataRepository;
import com.ptmall.app.net.base.ApiNetResponse;
import com.ptmall.app.utils.GlideUtil;
import com.ptmall.app.utils.PreferenceManager;
import com.ptmall.app.utils.SharedPreferenceUtil;
import com.xfly.app.bean.ImgBean;
import com.xfly.app.ui.Web;
import com.xfly.app.ui.Web2;
import com.xfly.app.ui.XfLoginActivity;

import java.util.List;

/**
 * @class 承载Fragment的界面
 * @anthor zyy mai:jingmengnihao@163.com
 * @time 2017/2/24  上午 11:02
 * @since 1.0
 */
public class WelcomActivity extends BaseMvpActivity {

    ApiDataRepository apiDataRepository = ApiDataRepository.getInstance();
    private static final String TAG = "ContentActivity";

    ImageView img;
    TextView txt;
    private int time=1;
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what == 1){
                Intent intent = new Intent(getActivity(), Web.class);
                intent.putExtra("url", HttpMethods.BASE_SITE + "/cn/home/index/");
                startActivity(intent);
                finish();
            }else {
                txt.setText("跳过"+time+"s");
                time--;
                if (time==0){
                    if (PreferenceManager.isFirst(WelcomActivity.this)) {
                        startActivity(new Intent(getContext(),XfLoginActivity.class));
                    } else {
                        startActivity(new Intent(getContext(),XfLoginActivity.class));
                    }
                    finish();
                    handler.removeMessages(0);
                }
                handler.sendEmptyMessageDelayed(0,1000);
            }

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setView(R.layout.activity_wel);
    }

    @Override
    public void initViews() {
        txt = (TextView) findViewById(R.id.txt);
        img = (ImageView) findViewById(R.id.img);
    }

    @Override
    public void initEvents() {
        apiDataRepository.getImg(null, new ApiNetResponse<List<ImgBean>>() {
            @Override
            public void onSuccess(List<ImgBean> data) {
                if(data!=null&&data.size()>0){
                    GlideUtil.showImg(WelcomActivity.this,data.get(0).cover,img);
                }
            }
        });
        img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent2 = new Intent(getContext(), WebActivity.class);
                intent2.putExtra("title","广告");
                intent2.putExtra("url", HttpMethods.BASE_URL+"Detail/guang");
                startActivity(intent2);
            }
        });
//        img.setImageResource(R.drawable.wel);
        txt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (PreferenceManager.isFirst(WelcomActivity.this)) {
                    startActivity(new Intent(getContext(),GuideActivity.class));
                } else {
                    startActivity(new Intent(getContext(),MainActivity.class));
                }
                finish();
                handler.removeMessages(0);
            }
        });
        if(!SharedPreferenceUtil.getSharedStringData(getContext(),"token").equals("")){

            handler.sendEmptyMessageDelayed(1,1000);
        }else {

            handler.sendEmptyMessageDelayed(0,1000);
        }
    }


    @Override
    public void onBackPressed() {
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeCallbacksAndMessages(null);
    }
}
